package edu.wayne.cs.csc4110.hw6.framework;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import edu.wayne.cs.csc4110.hw6.framework.Util.Constants;
import edu.wayne.cs.csc4110.hw6.framework.gui.lobby.GameButton;
import edu.wayne.cs.csc4110.hw6.framework.gui.login.LoginPanel;
import edu.wayne.cs.csc4110.hw6.framework.lobby.GameLobbyClient;
import edu.wayne.cs.csc4110.hw6.framework.lobby.GameLobbyServer;
import edu.wayne.cs.csc4110.hw6.framework.plugin.ActivePlugin;
import edu.wayne.cs.csc4110.hw6.framework.plugin.GamePluginLoop;
import edu.wayne.cs.csc4110.hw6.framework.plugin.PluginComboBoxRenderer;
import edu.wayne.cs.csc4110.hw6.framework.plugin.PluginLoader;
import edu.wayne.cs.csc4110.hw6.interfaces.Game;
import edu.wayne.cs.csc4110.hw6.interfaces.user.User;

/**
 * This Plugin will create the game frame when the user logs in
 */
public class GameCore implements Game {

	private JFrame gameFrame;
	private GameScreen screen;
	private LoginPanel loginPanel;

	public GameCore(ActivePlugin plugin) {
		loginPanel = new LoginPanel(plugin, this);
	}

	public static void main(String[] args)  {
		PluginLoader loader = new PluginLoader();
		final JComboBox<ActivePlugin> pluginComboBox = new JComboBox<ActivePlugin>( loader.getPlugins());
		pluginComboBox.setRenderer(new PluginComboBoxRenderer());
		
		final JFrame loaderFrame = new JFrame("Load Game");
		JPanel loaderPanel = new JPanel();
		JButton startBtn = new GameButton("Start");
		loaderPanel.add(pluginComboBox);
		loaderPanel.add(startBtn);
		loaderFrame.setPreferredSize(Constants.LOGINFRAME_PREFERRED_SIZE);
		loaderFrame.getContentPane().add(loaderPanel);
		loaderFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		loaderFrame.setLocationRelativeTo(null);
		loaderFrame.pack();
		loaderFrame.setVisible(true);

		startBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				ActivePlugin p = (ActivePlugin)pluginComboBox.getSelectedItem();
				GameCore core = new GameCore(p);
				core.startAuthentication(p);
				loaderFrame.dispose();
			}
		});
		
	}
	public void initialize(ActivePlugin plugin, User u) {

		ActivePlugin newPlugin = plugin.newInstance(u);
		screen = new GameScreen(newPlugin);
		GamePluginLoop gamePluginLoop = new GamePluginLoop(newPlugin, screen);
		gameFrame = new JFrame(newPlugin.getGameTitle());
		GameLobbyClient lobbyClient = new GameLobbyClient(u, gamePluginLoop,
				gameFrame);
		gameFrame.setPreferredSize(Constants.GAME_FRAME_PREFERRED_SIZE);
		getClass();
		gameFrame.setMinimumSize(Constants.GAME_FRAME_PREFERRED_SIZE);
		gameFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		gameFrame.setLayout(new BorderLayout());
		gameFrame.add(screen, BorderLayout.CENTER);
		gameFrame.add(lobbyClient, BorderLayout.SOUTH);
		gameFrame.getContentPane().add(screen);
		gameFrame.setVisible(true);
		gameFrame.setResizable(false);
		screen.requestFocus();
		lobbyClient.setTopFrame(gameFrame);
		GameLobbyServer.getInstance().addClient(lobbyClient);

	}

	@Override
	public void startAuthentication(ActivePlugin p) {

		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {

				JFrame authFrame = new JFrame("Login");
				authFrame.setPreferredSize(Constants.LOGINFRAME_PREFERRED_SIZE);
				authFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				authFrame.setLocationRelativeTo(null);
				authFrame.getContentPane().add(loginPanel);
				authFrame.pack();
				authFrame.setVisible(true);
			}

		});

	}

	@Override
	public void startPlugin(final ActivePlugin plugin, final User user) {
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				initialize(plugin, user);
			}

		});

	}

}
